package com.itheima.controller;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/toMainPage")
    public String toMainPage(){
        return "main";
    }

    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request){
        User dbUser = userService.login(user);
        if (dbUser == null ){
            //登录失败
             request.setAttribute("msg","用户或密码错误！");
            return "login";
        }else {
            //登录成功
            request.getSession().setAttribute("USER_SESSION",dbUser); //把用户信息保存到dbUser中
            return "redirect:/admin/main.jsp";
        }
    }
//注销功能
    @RequestMapping("/logout")
    public String logout(HttpSession session){
    session.invalidate();
    return "login";
    }
}
